package com.ws.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ws.commonutils.JwtUtils;
import com.ws.commonutils.R;
import com.ws.eduorder.entity.Order;
import com.ws.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request) {

        String orderNo=orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderNo", orderNo);
    }

    @GetMapping("getOrder/{orderNO}")
    public R getOrderInfo(@PathVariable String orderNO) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNO);
        Order order = orderService.getOne(queryWrapper);
        return R.ok().data("order", order);
    }

    @GetMapping("isBuy/{courseId}/{memeberId}")
    public Boolean isBuy(@PathVariable("courseId") String courseId,
                         @PathVariable("memeberId") String memeberId) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getCourseId, courseId);
        queryWrapper.eq(Order::getMemberId, memeberId);
        queryWrapper.eq(Order::getStatus, 1);
        int count = orderService.count(queryWrapper);
        return count > 0;
    }
}

